package com.ouyy.volunteerservice.volunteer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ouyy.volunteerservice.volunteer.domain.po.mediaComment;
import com.ouyy.volunteerservice.volunteer.service.VolunteerService;
import com.ouyy.volunteerservice.volunteer.service.mediaCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("/mediaComment") // 统一的路由前缀
public class mediaCommentController {

    @Autowired
    private mediaCommentService commentService;

    @Autowired
    private VolunteerService volunteerService;


    /**
     * 添加评论
     */
    @PostMapping("/add")
    public ResponseEntity<String> addComment(@RequestBody mediaComment comment) {


        boolean result = commentService.save(comment);
        return result
                ? ResponseEntity.ok("评论添加成功")
                : ResponseEntity.badRequest().body("评论添加失败");
    }

    /**
     * 根据ID删除评论
     */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteComment(@PathVariable Integer id) {
        boolean result = commentService.removeById(id);
        return result
                ? ResponseEntity.ok("评论删除成功")
                : ResponseEntity.badRequest().body("评论删除失败");
    }

    /**
     * 修改评论
     */
    @PutMapping("/update")
    public ResponseEntity<String> updateComment(@RequestBody mediaComment comment) {
        log.error("修改评论：{}", comment);
        boolean result = commentService.updateById(comment);
        return result
                ? ResponseEntity.ok("评论修改成功")
                : ResponseEntity.badRequest().body("评论修改失败");
    }

    /**
     * 根据ID查询评论
     */
    @GetMapping("/get/{id}")
    public ResponseEntity<mediaComment> getCommentById(@PathVariable Integer id) {
        Optional<mediaComment> comment = Optional.ofNullable(commentService.getById(id));
        return comment
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    /**
     * 查询所有评论
     */
    @GetMapping("/list")
    public ResponseEntity<List<mediaComment>> getAllComments() {
        List<mediaComment> comments = commentService.list();
        return ResponseEntity.ok(comments);
    }

    /**
     * 根据新闻ID查询评论列表
     */
    @GetMapping("/news/{commentId}")
    public ResponseEntity<List<mediaComment>> getCommentsByNewsId(@PathVariable Integer commentId) {

        List<mediaComment> comments = commentService.list(
                new QueryWrapper<mediaComment>().eq("comment_id", commentId)
        );
        for (mediaComment comment : comments){
            String username = volunteerService.getById(comment.getUserId()).getName();
            comment.setUsername(username);
        }
        return ResponseEntity.ok(comments);
    }
}
